<?php

define('TAO_API_PATH', PLUGIN_DIR . 'taobaoke' . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR);

/**
 * 配置
 */
function set_taobao_app() {
	include_once TAO_API_PATH . 'TopClient.php';
	include_once TAO_API_PATH. 'RequestCheckUtil.php';
	$tao   = new TopClient;
	$cache = new cache_file();
	$setting = $cache->get('taobaoke');
	$tao->appusers = $setting['appusers'];
	return $tao;
}
	
/**
 * 调用商品信息
 */
function get_taobao_item($id) {
	$name  = md5('item-' . $id);
	$data  = get_taobao_cache_data($name);
	if ($data) return $data;
	$tao = set_taobao_app();
	$cache = new cache_file();
	$setting = $cache->get('taobaoke');
	include_once TAO_API_PATH . 'request/TaobaokeItemsDetailGetRequest.php';
	$req = new TaobaokeItemsDetailGetRequest;
	$req->setFields('num_iid,title,nick,cid,desc,pic_url,num,location,price,click_url,shop_click_url,seller_credit_score');
	$req->setNumIids($id);
	$req->setNick($setting['username']);
	$resp = $tao->execute($req);
	$resp = object2array($resp);
	if (!isset($resp['taobaoke_item_details']['taobaoke_item_detail']) || empty($resp['total_results'])) return array();
	$data = $resp['taobaoke_item_details']['taobaoke_item_detail']['item'];
	$data['click_url'] = $resp['taobaoke_item_details']['taobaoke_item_detail']['click_url'];
	$data['seller_credit_score'] = $resp['taobaoke_item_details']['taobaoke_item_detail']['seller_credit_score'];
	$data['shop_click_url'] = $resp['taobaoke_item_details']['taobaoke_item_detail']['shop_click_url'];
	set_taobao_cache_data($name, $data);
	return $data;
}

/**
 * 调用商品列表 
 */
function get_taobao_list($cid, $sort='commissionNum_desc', $pagesize=10, $page=1 ,$keyword=null, $start_price=0, $end_price=0) {
	$name  = md5($cid . $sort . $pagesize . $page . $keyword . $start_price . $end_price);
	$data  = get_taobao_cache_data($name);
	if ($data) return $data;
	$tao = set_taobao_app();
	include_once TAO_API_PATH . 'request/TaobaokeItemsGetRequest.php';
	$req  = new TaobaokeItemsGetRequest;
	$cache = new cache_file();
	$setting = $cache->get('taobaoke');
	$req->setFields('num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume');
	$req->setNick($setting['username']);
	$req->setCid($cid);
	$req->setSort($sort);
	$req->setPageNo($page);
	$req->setPageSize($pagesize);
	if (!is_null($keyword)) $req->setKeyword($keyword);
	if ($end_price) {
		$req->setStartPrice($start_price);
		$req->setEndPrice($end_price);
	}
	$resp = $tao->execute($req);
	$resp = object2array($resp);
	if (isset($resp['total_results']) && $resp['total_results'] == 1) {
		$resp['taobaoke_items']['taobaoke_item'] = array(0=> $resp['taobaoke_items']['taobaoke_item']);
	}
	$data = isset($resp['taobaoke_items']['taobaoke_item']) ? $resp['taobaoke_items']['taobaoke_item'] : array();
	set_taobao_cache_data($name, $data);
	return $data;
}

/**
 * 调用商品类目 
 */
function _get_taobao_cats($cid=0) {
	$tao = set_taobao_app();
	include_once TAO_API_PATH . 'request/ItemcatsGetRequest.php';
	$req  = new ItemcatsGetRequest;
	$req->setFields('cid,parent_cid,name,is_parent');
	$req->setParentCid($cid);
	$resp = $tao->execute($req);
	$resp = object2array($resp);
	return isset($resp['item_cats']['item_cat']) ? $resp['item_cats']['item_cat'] : array();
}

/**
 * 调用商品类目(缓存) 
 */
function get_taobao_cats($cid=0) {
	$cache = new cache_file();
	$data  = $cache->get('taobaoke_cats');
	if (empty($data) || ($cid && !isset($data[$cid]))) return false;
	if (empty($cid)) {
	    $list = array();
	    foreach ($data as $k=>$t) {
		    if ($t['is_top']) $list[$k] = $t;
		}
		return $list;
	}
	return $data[$cid];
}

/**
 * 对象转换为数组
 */
function object2array($obj) { 
	$_arr = is_object($obj) ? get_object_vars($obj) : $obj; 
	if (is_array($_arr) && $_arr) {
		$arr = array();
		foreach ($_arr as $key => $val) { 
			$val = (is_array($val) || is_object($val)) ? object2array($val) : $val; 
			$arr[$key] = $val; 
		}
		return $arr;
	}
}
	
/**
 * get_cache
 */
function get_taobao_cache_data($name) { 
	$cache  = new cache_file();
	$name   = 'taobaoke' . DIRECTORY_SEPARATOR . $name;
	$data   = $cache->get($name);
	if (empty($data)) return false;
	$config = $cache->get('taobaoke');
	$time   = $config['cachetime'];
	if ($time) {
	    $ftime = filemtime(APP_ROOT . 'cache' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $name . '.cache.php');
		if (time() - $ftime >= $time * 60 * 60) return false;
	}
	return $data;
}

/**
 * set_cache
 */
function set_taobao_cache_data($name, $data) {
	$cache = new cache_file();
	$path  = APP_ROOT . 'cache' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'taobaoke' . DIRECTORY_SEPARATOR;
	if (!is_dir($path)) mkdir($path);
	$name  = 'taobaoke' . DIRECTORY_SEPARATOR . $name;
	$cache->set($name, $data);
}



?>